package cn.dckeji.dcssm.controller;


import cn.dckeji.dcssm.common.model.ResultModel;
import cn.dckeji.dcssm.entity.Menu;
import cn.dckeji.dcssm.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author Kenny
 * @since 2019-02-25
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 查询后台菜单
     * @return
     */
    @GetMapping("/getMenu")
    Map getMenu(String adminId) {
        return menuService.getMenu(adminId);
    }

    /**
     * 保存一级菜单
     * @param menu
     * @return
     */
    @PostMapping("/saveLevelOne")
    public ResultModel<Object> saveLevelOne(Menu menu) {
        menu.setGmtCreate(LocalDateTime.now());
        menu.setPid("-1");
        menuService.save(menu);
        return ResultModel.success();
    }

    /**
     * 保存二级菜单
     * @param menu
     * @return
     */
    @PostMapping("/saveLevelTwo")
    public ResultModel<Object> saveLevelTwo(Menu menu) {
        menu.setGmtCreate(LocalDateTime.now());
        menuService.save(menu);
        return ResultModel.success(menu.getId());
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     */
    @PostMapping("/deleteById")
    public ResultModel<Object> deleteById(String menuId) {
        menuService.deleteById(menuId);
        return ResultModel.success();
    }

    /**
     * 根据id查询菜单
     * @param menuId
     * @return
     */
    @GetMapping("/getById")
    public ResultModel<Menu> getById(String menuId) {
        Menu menu = menuService.getById(menuId);
        return ResultModel.success(menu);
    }


    /**
     * 更新菜单
     * @param menu
     * @return
     */
    @PostMapping("/update")
    public ResultModel<Object> update(Menu menu) {
        String menuId = menu.getId();
        Menu menuData = menuService.getById(menuId);
        menuData.setGmtModified(LocalDateTime.now());
        menuData.setHref(menu.getHref());
        menuData.setIcon(menu.getIcon());
        menuData.setTitle(menu.getTitle());
        menuService.updateById(menuData);
        return ResultModel.success();
    }
}

